package org.devocean.groupbuying.service.tasks;

import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.devocean.groupbuying.dao.ExploredDAO;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.domain.Explored;
import org.devocean.groupbuying.domain.Plan;
import org.devocean.groupbuying.domain.Product;
import org.devocean.groupbuying.domain.Promotion;
import org.devocean.groupbuying.service.DealService;
import org.devocean.groupbuying.service.ExploredService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ExplorerTasks {

	@Autowired
	private ExploredDAO exploredDAO;

	@Transactional(readOnly = true)
	// @Scheduled(cron = "* * * * * *")
	public void verifyExplored() {

		List<Explored> list = exploredDAO.verifyExplored();

		for (Explored explored : list) {
			Boolean deals = false;
			Boolean promotions = false;
			Product p = explored.getProduct();
			for (Deal deal : p.getDeals()) {
				if (deal.getStatus().equals("pending")
						|| deal.getStatus().equals("active")) {
					deals = true;
					System.out.println("deals found");
				}
			}

			for (Plan plan : p.getPlans()) {
				for (Promotion promotion : plan.getPromotions()) {
					if (Calendar.getInstance().getTime()
							.before(promotion.getExpireDate())) {
						promotions = true;
						System.out.println("promotions found");
					}
				}
			}

			if (deals || promotions) {
				System.out.println("Deals ON");
			} else {
				System.out.println("Deals OFF");
			}

			exploredDAO.updateExplored(explored);

		}

	}

}
